using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class AddPage : BasePage, ISiteSelected
{
    protected void Page_Load(object sender, EventArgs e)
    {
        wizAddPage.FinishButtonClick += new WizardNavigationEventHandler(wizAddPage_FinishButtonClick);
        if (!IsPostBack)
        {
            if (Request.QueryString["IsRoot"] is string)
            {
                try
                {
                    IsRoot = bool.Parse(Request.QueryString["IsRoot"]);
                }
                catch (FormatException)
                {
                }
            }

            if (IsRoot)
            {
                //remove first step
                wizAddPage.WizardSteps.RemoveAt(0);
            }
        }
    }

    void wizAddPage_FinishButtonClick(object sender, WizardNavigationEventArgs e)
    {
        if(Page.IsValid)
        {
            //Create the page
            Acms.Bll.Page page = new Acms.Bll.Page();
            page.SiteId = base.SiteId.Value;
            page.Name = txtName.Text;
            page.DisplayName = txtDisplayName.Text;

            //Gettheroot collection of the site and add this in
            Acms.Bll.PageCollection pages = null;
            if (IsRoot)
            {
                pages = Acms.Bll.PageCollection.GetRoot(base.SiteId.Value);
            }
            else
            {
                pages = Acms.Bll.PageCollection.GetByParentId(page.ParentId.Value);
            }
            pages.Add(page);
            pages.SaveChanges();
            Server.Transfer("~/pages.aspx");
        }
    }

    private Acms.Bll.Page _NewPage;
    protected Acms.Bll.Page NewPage
    {
        get
        {
            if (_NewPage == null)
            {
                _NewPage = new Acms.Bll.Page();
            }
            return _NewPage;
        }
    }

    protected bool IsRoot
    {
        get
        {
            if(ViewState["IsRoot"] is bool)
            {
                return (bool)ViewState["IsRoot"];
            }
            else
            {
                return false;
            }
        }
        set
        {
            ViewState["IsRoot"] = value;
        }
    }
}
